layui.use(['table', 'admin', 'ax','simTool','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var simtool = layui.simTool;
    var laydate = layui.laydate;

    /**
     * 会员用户管理
     */
    var Player = {
        tableId: "playerCountTable"
    };

    /**
     * 初始化表格的列
     */
    Player.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'id'},
            {field: 'userNo', sort: true, title: '用户编号'},
            {field: 'userName', sort: true, title: '用户名'},
            {field: 'amount', sort: true, title: '总金额(元)',
                templet:function (d) {
                    return Feng.formatCurrency(d.amount)
                }
            },
            {field: 'payCashAmount', sort: true, title: '支付金额（元）',
                templet:function (d) {
                    return Feng.formatCurrency(d.payCashAmount)
                }
            },
            {field: 'payScoreAmount', sort: true, title: '积分抵扣金额（元）',

            },
            {field: 'orderCount', sort: true, title: '购买次数（次）'},
            {field: 'shareUserCount', sort: true, title: '分享人数'},
            {field: 'allScore', sort: true, title: '历史积分'},
            {field: 'unUseScore', sort: true, title: '未使用积分'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };



    var  allScoreMin =$("#allScoreMin").val();
    var  allScoreMax =$("#allScoreMax").val();

    var  useScoreMin =$("#useScoreMin").val();
    var  useScoreMax =$("#useScoreMax").val();

    var  introCountMin =$("#introCountMin").val();
    var  introCountMax =$("#introCountMax").val();

    var  OrderMin =$("#OrderMin").val();
    var  OrderMax =$("#OrderMax").val();

    var  amountMin =$("#amountMin").val();
    var  amountMax =$("#amountMax").val();


    /**
     * 点击查询按钮
     */
    Player.search = function () {
        allScoreMin =$("#allScoreMin").val();
        allScoreMax =$("#allScoreMax").val();

        useScoreMin =$("#useScoreMin").val();
        useScoreMax =$("#useScoreMax").val();

         introCountMin =$("#introCountMin").val();
         introCountMax =$("#introCountMax").val();

         OrderMin =$("#OrderMin").val();
         OrderMax =$("#OrderMax").val();

          amountMin =$("#amountMin").val();
          amountMax =$("#amountMax").val();
        queryData = {};
            if(Player.verifyData(allScoreMin,allScoreMax)){
                queryData['allScoreMin'] = allScoreMin;
                queryData['allScoreMax'] = allScoreMax;
            }
            if(Player.verifyData(useScoreMin,useScoreMax)){
                queryData['unUseScoreMin'] = useScoreMin;
                queryData['unUseScoreMax'] = useScoreMax;
            }
            if(Player.verifyData(introCountMin,introCountMax)){
                queryData['introCountMin'] = introCountMin;
                queryData['introCountMax'] = introCountMax;
            }
            if(Player.verifyData(OrderMin,OrderMax)){
                queryData['orderMin'] = OrderMin;
                queryData['orderMax'] = OrderMax;
            }
            if(Player.verifyData(amountMin,amountMax)){
                queryData['amountMin'] = amountMin;
                queryData['amountMax'] = amountMax;
            }
        queryData['userNo'] = $("#userNo").val();
        table.reload(Player.tableId, {where: queryData
            ,page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };


    Player.handleData=function ( ) {
        createTimeMax = $("#createTimeMax").val();
        createTimeMin = $("#createTimeMin").val();

        if (!((createTimeMax == "" && createTimeMin == "") || (createTimeMax != "" && createTimeMin != ""))) {
            if (createTimeMin == "") {
                createTimeMin = "2000-01-01"
            }
            if (createTimeMax == "") {
                createTimeMax = Feng.currentDate();
            }
        }
    }


    /**
     *
     * @param min
     * @param max
     * @returns {boolean}
     */
    Player.verifyData=function (min,max ) {
        console.log(min)
        console.log(max)
        if(!((max==""&&min=="")||(max!="" && min!="")) ){

            Feng.info("请输入完整搜索区间")
            return false;
        }
        if(parseInt(max)  < parseInt(min)){
            Feng.info("请输入正确搜索区间")
        }
        return true;
    }



    /**
     * 导出excel按钮
     */
    Player.exportExcel = function () {
        var checkRows = table.checkStatus(Player.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };








    /**
     * 弹出添加对话框
     */
    Player.openDetailDlg = function (data) {
        admin.putTempData('formOk', false);
        admin.open({
            type: 2,
            title: '会员用户消费信息',
            maxmin:'true',
            area:['70%','70%'],
            content: Feng.ctxPath + '/player/countDetail?userNo='+data.userNo,
            end: function () {
                admin.getTempData('formOk') && table.reload(Player.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Player.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/player/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Player.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Player.tableId,
        url: Feng.ctxPath + '/player/consumeList',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: Player.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Player.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Player.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Player.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Player.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Player.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Player.onDeleteItem(data);
        }else if (layEvent === 'detail') {
            Player.openDetailDlg(data);
        }
    });
});
